package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.ChangePasswordRequest;
import com.mathlearning.auth.dto.UpdateUserProfileRequest;
import com.mathlearning.auth.dto.UploadResponse;
import com.mathlearning.auth.dto.UserInfo;
import com.mathlearning.auth.dto.user.FollowResponse;
import com.mathlearning.auth.dto.user.UserStatsResponse;
import com.mathlearning.auth.entity.UserStats;
import com.mathlearning.auth.service.FollowService;
import com.mathlearning.auth.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;
    
    @Autowired
    private FollowService followService;

    /**
     * 获取用户资料
     */
    @GetMapping("/profile")
    public ApiResponse<UserInfo> getUserProfile(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        UserInfo userInfo = userService.getUserProfile(userId);
        return ApiResponse.success(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping("/profile")
    public ApiResponse<UserInfo> updateUserProfile(
            @Valid @RequestBody UpdateUserProfileRequest updateRequest,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        UserInfo userInfo = userService.updateUserProfile(userId, updateRequest);
        return ApiResponse.success("修改成功", userInfo);
    }

    /**
     * 上传头像
     */
    @PostMapping("/upload-avatar")
    public ApiResponse<UploadResponse> uploadAvatar(
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        UploadResponse response = userService.uploadAvatar(userId, file);
        return ApiResponse.success("上传成功", response);
    }

    /**
     * 修改密码
     */
    @PostMapping("/change-password")
    public ApiResponse<?> changePassword(
            @Valid @RequestBody ChangePasswordRequest changePasswordRequest,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        userService.changePassword(userId, changePasswordRequest);
        return ApiResponse.success("密码修改成功，请重新登录");
    }

    /**
     * 获取用户学习统计（旧版本）
     */
    @GetMapping("/stats")
    public ApiResponse<UserStats> getUserStats(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        UserStats stats = userService.getUserStats(userId);
        return ApiResponse.success(stats);
    }
    
    /**
     * 获取用户卡片统计数据
     */
    @GetMapping("/{userId}/stats")
    public ApiResponse<UserStatsResponse> getUserStatsCard(
            @PathVariable Long userId,
            HttpServletRequest request) {
        Long currentUserId = (Long) request.getAttribute("userId");
        UserStatsResponse stats = userService.getUserStatsResponse(userId, currentUserId);
        return ApiResponse.success(stats);
    }
    
    /**
     * 关注/取消关注用户
     */
    @PostMapping("/{userId}/follow")
    public ApiResponse<FollowResponse> followUser(
            @PathVariable Long userId,
            HttpServletRequest request) {
        Long currentUserId = (Long) request.getAttribute("userId");
        FollowResponse response = followService.toggleFollow(currentUserId, userId);
        return ApiResponse.success("操作成功", response);
    }
}

